package com.engineering.elink.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.common.Constant;
import com.engineering.elink.dao.TokenAuthenticationMapper;
import com.engineering.elink.service.RedisService;
import com.engineering.elink.service.TokenAuthenticationService;

@Service("tokenAuthenticationService")
public class TokenAuthenticationServiceImpl implements TokenAuthenticationService {
	@Autowired
	private RedisService redisService;
	@Autowired
	private TokenAuthenticationMapper tokenAuthenticationMapper;

	@Override
	public String getAuthTokenInfo(String seed, String authToken) {
		@SuppressWarnings("unchecked")
		Map<String, Object> mapInRedis = (Map<String, Object>) redisService.getObjectBySeed(Constant.tokenAuthenticationCache, seed);
		if (mapInRedis != null && mapInRedis.get("object") != null) {
			System.out.println(555);
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) mapInRedis.get("object");
			if (map != null) {
				System.out.println(666);
				String authTokenInCache = (String) map.get("authToken");
				if (authToken.equals(authTokenInCache)) {
					System.out.println(777);
					return (String) map.get("userId");
				}
			} else {
				return null;
			}
//		} else {
//			System.out.println(111);
//			TokenAuthentication tokenAuthentication = tokenAuthenticationMapper.selectBySeed(seed);
//			if (tokenAuthentication != null) {
//				System.out.println(222);
//				if (authToken.equals(tokenAuthentication.getToken())) {
//					System.out.println(333);
//					if (tokenAuthentication.getExpireTime().getTime() > new Date().getTime()) {
//						System.out.println(444);
//						return tokenAuthentication.getUserId();
//					}
//				}
//			}
		}
		return null;
	}

}
